//using UnityEngine;
using System;
using System.Collections;
using System.Text;

namespace MxNetwork
{
    public class MxNwkAddressIP
    {
        public MxNwkAddressIP()
        {
            Sections[0] = 127;
            Sections[1] = 0;
            Sections[2] = 0;
            Sections[3] = 1;
        }

        public MxNwkAddressIP(byte b1, byte b2, byte b3, byte b4)
        {
            Sections[0] = b1;
            Sections[1] = b2;
            Sections[2] = b3;
            Sections[3] = b4;
        }

        public MxNwkAddressIP(MxNwkAddressIP v)
	    {
            Sections[0] = v.Sections[0];
            Sections[1] = v.Sections[1];
            Sections[2] = v.Sections[2];
            Sections[3] = v.Sections[3];
	    }

        public MxNwkAddressIP(string v)
        {
            string[] result = v.Split(new char[] { '.' });            
            Sections[0] = Convert.ToByte(result[0]);
            Sections[1] = Convert.ToByte(result[1]);
            Sections[2] = Convert.ToByte(result[2]);
            Sections[3] = Convert.ToByte(result[3]);               
        }

        public static bool operator==(MxNwkAddressIP lhs, MxNwkAddressIP rhs)
        {
            return ((lhs.Sections[0] == rhs.Sections[0]) && (lhs.Sections[1] == rhs.Sections[1]) && (lhs.Sections[2] == rhs.Sections[2]) && (lhs.Sections[3] == rhs.Sections[4]));
        }

        public static bool operator !=(MxNwkAddressIP lhs, MxNwkAddressIP rhs)
        {
            return ((lhs.Sections[0] != rhs.Sections[0]) || (lhs.Sections[1] != rhs.Sections[1]) || (lhs.Sections[2] != rhs.Sections[2]) || (lhs.Sections[3] != rhs.Sections[4]));
        }
		
		public override int GetHashCode() 
		{
			return base.GetHashCode();
		}
		
		public override bool Equals(object obj) 
		{
			return base.Equals(obj);
		}

        public string GetIP()
        {
            return (Sections[0].ToString() + "." + Sections[1].ToString() + "." + Sections[2].ToString() + "." + Sections[3].ToString());
        }

        public static bool StrTo(string src, MxNwkAddressIP dest)
        {
            string[] result = src.Split(new char[]{'.'});
            if(result.Length != 4)
            {
                return false;
            }
            else
            {
                dest.Sections[0] = Convert.ToByte(result[0]);
                dest.Sections[1] = Convert.ToByte(result[1]);
                dest.Sections[2] = Convert.ToByte(result[2]);
                dest.Sections[3] = Convert.ToByte(result[3]);
                return true;
            }
        }

        public byte[] Sections = new byte[4]; 
    }
}
